package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Config.AliMessage;
import com.realtyManage.springboot.Dao.*;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Model.Vo.CustFlowEntity;
import com.realtyManage.springboot.Model.Vo.CustomerEntity;
import com.realtyManage.springboot.Service.*;
import com.realtyManage.springboot.Utils.CoreUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING;

/**
 * IndexServiceImpl
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/3/22
 **/
@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private CustFlowEventRepository custFlowEventRepository;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAnnouncementService announcementService;
    @Autowired
    private IIndexService indexService;
    @Autowired
    private IChargeDetailService chargeDetailService;

    @Autowired
    private CustomerRecordRepository customerRecordRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CustomerRegistrationRepository customerRegistrationRepository;

    @Override
    public Customer getCustomerDetail(Integer cid) {
        return customerRepository.getOne(cid);
    }

    @Override
    public Page<Customer> findPagedByWhereClause(CustomerEntity customerEntity) {
        Integer pageNumber = customerEntity.getPageNumber()-1;
        Integer pageSize = customerEntity.getPageSize();

        //排序条件
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        if(customerEntity.getSort() != null){
            if (customerEntity.getSort().equals("DESC")) {
                sort = new Sort(Sort.Direction.DESC, customerEntity.getSortField());
            } else if(customerEntity.getSort().equals("ASC")){
                sort = new Sort(Sort.Direction.ASC, customerEntity.getSortField());
            }
        }
        Pageable pageable = PageRequest.of(pageNumber,pageSize,sort);

        //动态查询条件
        Specification<Customer> specification = (Specification<Customer>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (customerEntity.getConnUid()!=null) {
                Predicate p1 = criteriaBuilder.equal(root.get("connUserId"),customerEntity.getConnUid());
                list.add(p1);
            }
            if (customerEntity.getTelephone()!=null) {
                Predicate p2 = criteriaBuilder.like(root.get("telephone"),"%"+customerEntity.getTelephone()+"%");
                list.add(p2);
            }
            if (customerEntity.getName()!=null) {
                Predicate p3 = criteriaBuilder.like(root.get("name"),customerEntity.getName());
                list.add(p3);
            }
            if (customerEntity.getStatus()!=null) {
                Predicate p3 = criteriaBuilder.equal(root.get("status"),customerEntity.getStatus());
                list.add(p3);
            }
            if (customerEntity.getKeepType()!=null) {
                Predicate p4 = criteriaBuilder.equal(root.get("type"),customerEntity.getKeepType());
                list.add(p4);
            }
            if (customerEntity.getConnHouseId()!=null) {
                Predicate p5 = criteriaBuilder.equal(root.get("connHouseId"),customerEntity.getConnHouseId());
                list.add(p5);
            }
            if (customerEntity.getBusinessManagerIds()!=null){
                Expression<String> exp = root.get("connUserId");
                list.add(exp.in(customerEntity.getBusinessManagerIds()));
            }
            if (customerEntity.getHouseIds()!=null){
                Expression<String> exp = root.get("connHouseId");
                list.add(exp.in(customerEntity.getHouseIds()));
            }
            //是否为审核状态
            /*Expression<String> exp = root.get("reportedType");
            Integer type = 0;
            list.add(exp.in(type));*/
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        Page<Customer> lst = customerRepository.findAll(specification, pageable);
        return customerRepository.findAll(specification, pageable);
    }

    @Override
    public boolean updateCustomerInfo(CustFlowEntity entity, HttpServletRequest request) {
        SimpleDateFormat sda = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustFlowEvent custFlowEvent = new CustFlowEvent();
        //更新Customer记录
        Customer customer = customerRepository.findById(entity.getCustId()).orElse(null);
        customer.setStatus(entity.getOperateCode());
        customer.setLastOperateDate(new Date());

        //生成CustFlowEvent记录
        User user = userService.findByTelePhone(request.getSession().getAttribute("user").toString());
        custFlowEvent.setConnCustId(customer.getId());
        custFlowEvent.setOperateCode(entity.getOperateCode());
        custFlowEvent.setHandleUserId(user.getId());
        custFlowEvent.setHandleTime(new Date());
        custFlowEvent.setRemark(entity.getRemark());
        String word = "";
        String specialWord = "";
        Integer type = null;
        switch (entity.getOperateCode()){
            case 2://已到访
                type = 5;
                word = "带访";
                custFlowEvent.setTime(sda.format(new Date()));

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                /*try {
                    customer.setKeepTime(format.parse(CoreUtils.plusDay2(indexService.getHouseDetail(customer.getConnHouseId()).getValidDate())));
                } catch (ParseException e) {
                    e.printStackTrace();
                }*/
                break;
            case 3://已认购
                type=7;
                word = "定购";
                custFlowEvent.setBuyMoney(entity.getBuyMoney());
                custFlowEvent.setEarnestMoney(entity.getEarnestMoney());
                custFlowEvent.setExpectSignDate(entity.getExpectSignDate());
                custFlowEvent.setSubscriptionBook(entity.getSubscriptionBook());
                custFlowEvent.setTime(sda.format(new Date()));
                break;
            case 4://已签约
                type=6;
                word = "签约";
                custFlowEvent.setRealMoney(entity.getRealMoney());
                custFlowEvent.setFirstMoney(entity.getFirstMoney());
                custFlowEvent.setCommissionTime(entity.getCommissionTime());
                custFlowEvent.setContract(entity.getContract());
                custFlowEvent.setTime(sda.format(new Date()));

                //佣金明细表增加记录
                chargeDetailService.addNewRecord(entity.getCustId(),entity.getRealMoney(),1);

                String result = AliMessage.sendMessage(1, customer.getName(),userService.findById(customerRepository.getOne(customer.getId()).getConnUserId()).getTelephone());
                if (result != null && result.equals("OK")) {
                    System.out.println("短信发送成功");
                } else {
                    System.out.println("短信发送失败");
                }
                break;
            case 5://已退房
                custFlowEvent.setReturnPic(entity.getReturnPic());
                custFlowEvent.setTime(sda.format(new Date()));

                //更新佣金为无效
                chargeDetailService.updateRecord(entity.getCustId(),null,2);
                break;
            case 6://无效
                break;
            case 7://已结佣
                type=8;
                word = "成交";
                CustFlowEvent tmpEntity = custFlowEventRepository.findByConnCustIdAndOperateCode(entity.getCustId(),4);
                //HouseDetail houseDetail = indexService.getHouseDetail(customer.getConnHouseId());
                Double shopChargeRate = (customer.getShopChargeRate()!=null?customer.getShopChargeRate()/100:0.00);
                Double shopChargeFixed = (customer.getShopChargeFixed()!=null?customer.getShopChargeFixed():0.00);
                String shopChargeExtra = (customer.getShopChargeExtra()!=null?customer.getShopChargeExtra():"");

                Double personChargeRate = (customer.getPersonChargeRate()!=null?customer.getPersonChargeRate()/100:0.00);
                Double personChargeFixed = (customer.getPersonChargeFixed()!=null?customer.getPersonChargeFixed():0.00);
                String personChargeExtra = (customer.getPersonChargeExtra()!=null?customer.getPersonChargeExtra():"");

                Double indChargeRate = (customer.getIndChargeRate()!=null?customer.getIndChargeRate()/100:0.00);
                Double indChargeFixed = (customer.getIndChargeFixed()!=null?customer.getIndChargeFixed():0.00);
                String indChargeExtra = (customer.getIndChargeExtra()!=null?customer.getIndChargeExtra():"");

                Double money = 0.00;
                String realCharge = "";
                user = userService.findById(customer.getConnUserId());
                if(user.getLevel()==4){//门店负责人
                    money = tmpEntity.getRealMoney()*shopChargeRate/100+shopChargeFixed;
                    realCharge += money.toString() + "  " + shopChargeExtra;
                }else if(user.getLevel()==3){//门店经纪人
                    money = tmpEntity.getRealMoney()*personChargeRate/100+personChargeFixed;
                    realCharge += money.toString() + "  " + personChargeExtra;
                }else if(user.getLevel()==2){//独立经纪人
                    money = tmpEntity.getRealMoney()*indChargeRate/100+indChargeFixed;
                    realCharge += money.toString() + "  " + indChargeExtra;
                }
                specialWord = "得佣金"+realCharge;
                custFlowEvent.setCommissionPic(entity.getCommissionPic());
                custFlowEvent.setTime(sda.format(new Date()));

                //更新佣金为已结算
                chargeDetailService.updateRecord(entity.getCustId(),2,null);

                //若为独立经纪人成交则新增上线提成
                if(user.getLevel()==2){
                    chargeDetailService.addNewRecord(entity.getCustId(),tmpEntity.getRealMoney(),2);
                }
                //若为门店经纪人成交则新增门店负责人提成
                if(user.getLevel()==3){
                    double shopManagerCharge = tmpEntity.getRealMoney()*shopChargeRate/100+shopChargeFixed;
                    chargeDetailService.addNewRecord(entity.getCustId(),tmpEntity.getRealMoney(),3);
                }

                break;
            default:
                 break;
        }

        if(custFlowEventRepository.save(custFlowEvent)!=null && customerRepository.save(customer) != null){
            if(type!=null){
                Announcement announcement = new Announcement();
                User jjr = userService.findById(customer.getConnUserId());
                String title = "";
                if(jjr.getLevel()==4){//门店
                    title = jjr.getName()+"门店负责人"+word+customer.getName()+"客户一名"+specialWord;

                }
                if(jjr.getLevel()==3){//经纪人
                    title = jjr.getName()+"门店经纪人"+word+customer.getName()+"客户一名"+specialWord;
                }
                if(jjr.getLevel()==2){//独立
                    title = jjr.getName()+"独立经纪人"+word+customer.getName()+"客户一名"+specialWord;
                }
                announcement.setTitle(title);
                announcement.setType(type);
                announcement.setTime(new Date());
                announcement.setState(1);
                announcementService.insert(announcement);
            }
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Customer> findByTelephone(String telephone) {
        return customerRepository.findByTelephone(telephone);
    }

    @Override
    public List<Customer> findByTelephoneAndConnHouseIdAndStatus(String telephone,int connHouseId, int status) {
        return customerRepository.findByTelephoneAndConnHouseIdAndStatus(telephone,connHouseId,status);
    }

    @Override
    public void updateKeepRecordInvalid(Customer customer) {
        customer.setStatus(6);
        customerRepository.saveAndFlush(customer);
    }

    @Override
    public Customer insert(Customer entity) {
        return customerRepository.saveAndFlush(entity);
    }

    @Override
    public int countByConnUserIdAndType(Integer id, int type) {
        return customerRepository.countByConnUserIdAndType(id,type);
    }

    @Override
    public int countByConnUserIdAndStatusNotAndStatusNot(Integer id, int status1, int status2) {
        return customerRepository.countByConnUserIdAndStatusNotAndStatusNot(id,status1,status2);
    }

    @Override
    public int countByConnUserIdAndState(Integer id, int state) {
        return customerRepository.countByConnUserIdAndStatus(id,state);
    }

    @Override
    public void automaticBatchUpdateRecordByViewTime(int status) {
        customerRepository.updateByStatusAndViewTimeGatherThan(status);
    }

    @Override
    public void automaticBatchUpdateRecordByKeepTime(int status) {
        customerRepository.updateByStatusAndKeepTimeGatherThan(status);
    }

    @Override
    public Page<Customer> findPagedByConnHouseId(int connHouseId, int pageNumber, int pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable =new PageRequest(pageNumber - 1, pageSize, sort);
        return customerRepository.findByConnHouseId(pageable,connHouseId);
    }

    @Override
    public List<String> findByNameLike(String businessManagerName) {
        return customerRepository.findByNameLike(businessManagerName);
    }

    @Override
    public int countByConnUserIdAndStatusAndLastOperateDateBetween(Integer uid, int status, Date startTime, Date endTime) {
        return customerRepository.countByConnUserIdAndStatusAndLastOperateDateBetween(uid,status,startTime,endTime);
    }

    @Override
    public List<Customer>  getCustomerList(Integer houseId,String level){
        return  customerRepository.getCustomerList(houseId,level);
    }

    @Override
    public  int updateReportedType(String customerId,String userName,String userId,Date approvalTime,String reportedType){
        return  customerRepository.updateReportedType(customerId,userName,userId,approvalTime,reportedType);
    }

    @Override
    public String getName(String customerId){
        return  customerRepository.getName(customerId);
    }

    @Override
    public int addCustomerRecord(CustomerRecord customerRecord) {
        return customerRecordRepository.save(customerRecord).getId();
    }

    @Override
    public int importCustomerList(MultipartFile file, HttpServletRequest request) {
        List<HouseDetail> list = new ArrayList<HouseDetail>();
        //1.得到上传的表
        Workbook workbook2 = null;
        try {
            workbook2 = WorkbookFactory.create(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        //2、获取sheet1工作表
        Sheet sheet2 = workbook2.getSheet("Sheet1");
        //获取表的总行数
        int num = sheet2.getLastRowNum();
        //总列数
        int col = sheet2.getRow(0).getLastCellNum();
        //遍历excel每一行
        for (int j = 1; j < num + 1; j++) {
            Row row1 = sheet2.getRow(j);
            //获取表中第i行，第1列的单元格
            Cell cell3 = row1.getCell(0);  //姓名
            if (cell3 != null){
                cell3.setCellType(CELL_TYPE_STRING);
            }
            Cell cell4 = row1.getCell(1);  //手机号
            if(cell4 != null){
                cell4.setCellType(CELL_TYPE_STRING);
            }
            Cell cell5 = row1.getCell(2);  //地址
            if (cell5 != null){
                cell5.setCellType(CELL_TYPE_STRING);
            }
            Cell cell6 = row1.getCell(3);  //客户来源
            if (cell6 != null){
                cell6.setCellType(CELL_TYPE_STRING);
            }
            Cell cell7 = row1.getCell(4);  //客户年纪
            if (cell7 != null){
                cell7.setCellType(CELL_TYPE_STRING);
            }
            Cell cell8 = row1.getCell(5);  //意向物业类型
            if (cell8 != null){
                cell8.setCellType(CELL_TYPE_STRING);
            }
            Cell cell9 = row1.getCell(6);  //区域
            if(cell9 != null){
                cell9.setCellType(CELL_TYPE_STRING);
            }
            Cell cell10 = row1.getCell(7);  //获知途径
            if (cell10 != null){
                cell10.setCellType(CELL_TYPE_STRING);
            }
            Cell cell11 = row1.getCell(8);  //面积需求
            if (cell11 != null){
                cell11.setCellType(CELL_TYPE_STRING);
            }
            Cell cell12 = row1.getCell(9);  //总价需求
            if (cell12 != null){
                cell12.setCellType(CELL_TYPE_STRING);
            }
            Cell cell13 = row1.getCell(10);  //置业次数
            if (cell13 != null){
                cell13.setCellType(CELL_TYPE_STRING);
            }
            Cell cell14 = row1.getCell(11);  //户型需求
            if (cell14 != null){
                cell14.setCellType(CELL_TYPE_STRING);
            }
            Cell cell15 = row1.getCell(12);  //考虑因素
            if (cell15 != null){
                cell15.setCellType(CELL_TYPE_STRING);
            }
            Cell cell16 = row1.getCell(13);  //价格接收度
            if (cell16 != null){
                cell16.setCellType(CELL_TYPE_STRING);
            }
            Cell cell17 = row1.getCell(14);  //意向户型
            if (cell17 != null){
                cell17.setCellType(CELL_TYPE_STRING);
            }
            Cell cell18 = row1.getCell(15);  //拒绝原因
            if (cell18 != null){
                cell18.setCellType(CELL_TYPE_STRING);
            }
            Cell cell19 = row1.getCell(16);  //客户职业
            if (cell19 != null){
                cell19.setCellType(CELL_TYPE_STRING);
            }
            Cell cell20 = row1.getCell(17);  //家庭结构
            if (cell20 != null){
                cell20.setCellType(CELL_TYPE_STRING);
            }
            Cell cell21 = row1.getCell(18);  //客户意向
            if (cell21 != null){
                cell21.setCellType(CELL_TYPE_STRING);
            }
            Cell cell22 = row1.getCell(19);  //是否看过样板房
            if (cell22 != null){
                cell22.setCellType(CELL_TYPE_STRING);
            }
            Cell cell23 = row1.getCell(20);  //来访记录
            if (cell23 != null){
                cell23.setCellType(CELL_TYPE_STRING);
            }
            Cell cell24 = row1.getCell(21);  //客户状态
            if (cell24 != null){
                cell24.setCellType(CELL_TYPE_STRING);
            }
            Cell cell25 = row1.getCell(22);  //所属职业顾问     ----1.2.3
            if (cell25 != null){
                cell25.setCellType(CELL_TYPE_STRING);
            }

            //这里new 一个对象，用来装填从页面上传的Excel数据，字段根据上传的excel决定
            CustomerRegistration excel= new CustomerRegistration();
            if (cell3 != null){
                excel.setName(cell3.toString());
            }
            if (cell4 != null){
                excel.setPhone(cell4.toString());
            }
            if (cell5 != null){
                excel.setAddress(cell5.toString());
            }
            if (cell6 != null){
                excel.setCustomerSource(cell6.toString());
            }
            if (cell7 != null){
                excel.setAge(Integer.valueOf(cell7.toString()));
            }
            if (cell8 != null){
                excel.setIntentionPropertyType(cell8.toString());
            }
            if (cell9 != null){
                excel.setAreaName(cell9.toString());
            }
            if (cell10 != null){
                excel.setAccessKnowledge(cell10.toString());
            }
            if (cell11 != null){
                excel.setAreaName(cell11.toString());
            }
            if (cell12 != null){
                excel.setTotalNeed(cell12.toString());
            }
            if (cell13 != null){
                excel.setHomeOwnership(cell13.toString());
            }
            if (cell14 != null){
                excel.setHouseTypeNeed(cell14.toString());
            }
            if (cell15 != null){
                excel.setConsiderType(cell15.toString());
            }
            if (cell16 != null){
                excel.setPriceAcceptance(cell16.toString());
            }
            if (cell17 != null){
                excel.setIntentionalHouseholdType(cell17.toString());
            }
            if (cell18 != null){
                excel.setRefuseReason(cell18.toString());
            }
            if (cell19 != null){
                excel.setCustomerOccupation(cell19.toString());
            }
            if (cell20 != null){
                excel.setFamilyStructure(cell20.toString());
            }
            if (cell21 != null){   //应用字典表中的客户类型
                if (cell21.toString().equals("a") || cell21.toString().equals("A")){
                    excel.setCustomerIntention("244");
                }else if(cell21.toString().equals("b") || cell21.toString().equals("B")){
                    excel.setCustomerIntention("245");
                }else if (cell21.toString().equals("c") || cell21.toString().equals("C")){
                    excel.setCustomerIntention("246");
                }else if (cell21.toString().equals("d") || cell21.toString().equals("D")){
                    excel.setCustomerIntention("247");
                }else if (cell21.toString().equals("e") || cell21.toString().equals("E")){
                    excel.setCustomerIntention("248");
                }
            }
            if (cell22 != null){
                excel.setModelHouses(cell22.toString());
            }
            /*if (!cell23.equals("")){
                excel.setDiscountPrice(Integer.valueOf(cell23.toString()));    //另外一张记录表
            }*/
            if (cell24 != null){
                excel.setCustomerType(cell24.toString());
            }
            if (cell25 != null){
                excel.setReceptionName(cell25.toString());
                excel.setReceptionId(userService.queryUserId(cell25.toString()));
                //数据库中没有此用户则不能正常操作
            }
            //默认操作
            excel.setComeTime(new Date());
            excel.setEffective(0);
            excel.setLevel("D");
            excel.setIdentify(0);
            excel.setIdentifyMoney("0");
            excel.setLargeSum(0);
            excel.setLargeSumMoney("0");
            excel.setIsSystem(0);
            excel.setVisit(1);
            excel.setIncomingCall(1);
            excel.setInvitation(0);
            excel.setExpandCustomer(0);
            excel.setTotalContract("0");
            excel.setMoneyBack("0");
            excel.setDownPayments(0);
            excel.setDownPaymentsMoney("0");
            excel.setPaymentTypeMoney("0");
            //判断用户手机号重复出现
            boolean tf = userService.isExistPhone(excel.getPhone());
            int cId = 0;
            if (!tf){
                 cId = customerRegistrationRepository.save(excel).getId();
            }
            //添加客户来访记录
            if (cId == 0){
                cId = userRepository.queryUser(excel.getPhone());
            }
            //可能没有填写手机号
            if (cId == 0){
                cId = userRepository.queryUserId(excel.getName());
            }
            CustomerRecord cr = new CustomerRecord();
            if (cell23 != null){
                cr.setContent(cell23.toString());
            }
            cr.setCustomId(cId);
            cr.setInfoTime(new Date());
            cr.setUid(excel.getReceptionId());
            cr.setType(0);
            customerRecordRepository.save(cr);
        }
        return 0;
    }

   /* @Override
    public int importCustomerList(MultipartFile file) {
        List<CustomerRegistration> list = new ArrayList<CustomerRegistration>();
        //1.得到上传的表
        Workbook workbook2 = null;
        try {
            workbook2 = WorkbookFactory.create(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        //2、获取sheet1工作表
        Sheet sheet2 = workbook2.getSheet("客户登记表");
        //获取表的总行数
        int num = sheet2.getLastRowNum();
        //总列数
        int col = sheet2.getRow(0).getLastCellNum();
        //遍历excel每一行
        for (int j = 1; j < num + 1; j++) {
            Row row1 = sheet2.getRow(j);
            //获取表中第i行，第1列的单元格
            Cell cell3 = row1.getCell(0);  //客户姓名
            if (cell3 != null){
                cell3.setCellType(CELL_TYPE_STRING);
            }
            Cell cell4 = row1.getCell(1);  //手机号
            if(cell4 != null){
                cell4.setCellType(CELL_TYPE_STRING);
            }
            Cell cell5 = row1.getCell(2);  //地址
            if (cell5 != null){
                cell5.setCellType(CELL_TYPE_STRING);
            }
            Cell cell6 = row1.getCell(3);  //客户来源
            if (cell6 != null){
                cell6.setCellType(CELL_TYPE_STRING);
            }
            Cell cell7 = row1.getCell(4);  //客户年纪
            if (cell7 != null){
                cell7.setCellType(CELL_TYPE_STRING);
            }
            Cell cell8 = row1.getCell(5);  //意向物业类型
            if (cell8 != null){
                cell8.setCellType(CELL_TYPE_STRING);
            }
            Cell cell9 = row1.getCell(6);  //区域
            if(cell9 != null){
                cell9.setCellType(CELL_TYPE_STRING);
            }
            Cell cell10 = row1.getCell(7);  //到访次数
            if (cell10 != null){
                cell10.setCellType(CELL_TYPE_STRING);
            }
            Cell cell11 = row1.getCell(8);  //获知途径
            if (cell11 != null){
                cell11.setCellType(CELL_TYPE_STRING);
            }
            Cell cell12 = row1.getCell(9);  //面积需求
            if (cell12 != null){
                cell12.setCellType(CELL_TYPE_STRING);
            }
            Cell cell13 = row1.getCell(10);  //总价需求
            if (cell13 != null){
                cell13.setCellType(CELL_TYPE_STRING);
            }
            Cell cell14 = row1.getCell(11);  //置业次数
            if (cell14 != null){
                cell14.setCellType(CELL_TYPE_STRING);
            }
            Cell cell15 = row1.getCell(12);  //付款方式
            if (cell15 != null){
                cell15.setCellType(CELL_TYPE_STRING);
            }
            Cell cell16 = row1.getCell(13);  //户型需求
            if (cell16 != null){
                cell16.setCellType(CELL_TYPE_STRING);
            }
            Cell cell17 = row1.getCell(14);  //考虑因素
            if (cell17 != null){
                cell17.setCellType(CELL_TYPE_STRING);
            }
            Cell cell18 = row1.getCell(15);  //价格接受度
            if (cell18 != null){
                cell18.setCellType(CELL_TYPE_STRING);
            }
            Cell cell19 = row1.getCell(16);  //意向户型
            if (cell19 != null){
                cell19.setCellType(CELL_TYPE_STRING);
            }
            Cell cell20 = row1.getCell(17);  //无效原因
            if (cell20 != null){
                cell20.setCellType(CELL_TYPE_STRING);
            }
            Cell cell21 = row1.getCell(18);  //客户职业
            if (cell21 != null){
                cell21.setCellType(CELL_TYPE_STRING);
            }
            Cell cell22 = row1.getCell(19);  //家庭结构
            if (cell22 != null){
                cell22.setCellType(CELL_TYPE_STRING);
            }
            Cell cell23 = row1.getCell(20);  //优惠单价
            if (cell23 != null){
                cell23.setCellType(CELL_TYPE_STRING);
            }
            Cell cell24 = row1.getCell(21);  //优惠总价
            if (cell24 != null){
                cell24.setCellType(CELL_TYPE_STRING);
            }
            Cell cell25 = row1.getCell(22);  //关注度
            if (cell25 != null){
                cell25.setCellType(CELL_TYPE_STRING);
            }
            Cell cell26 = row1.getCell(23);  //房源状态     ----1.2.3
            if (cell26 != null){
                cell26.setCellType(CELL_TYPE_STRING);
            }
            Cell cell27 = row1.getCell(24);  //销售员
            if (cell27 != null){
                cell27.setCellType(CELL_TYPE_STRING);
            }
            Cell cell28 = row1.getCell(25);  //业主名
            if (cell28 != null){
                cell28.setCellType(CELL_TYPE_STRING);
            }
            Cell cell29 = row1.getCell(26);  //备注
            if (cell29 != null){
                cell29.setCellType(CELL_TYPE_STRING);
            }
            Cell cell30 = row1.getCell(27);  //领导建议
            if (cell30 != null){
                cell30.setCellType(CELL_TYPE_STRING);
            }
            // Cell cell31 = row1.getCell(28);  //操作

            //这里new 一个对象，用来装填从页面上传的Excel数据，字段根据上传的excel决定
            HouseDetail excel= new HouseDetail();
            if (cell3 != null){
                excel.setStage(cell3.toString());
            }
            if (cell4 != null){
                excel.setBlockCode(Integer.valueOf(cell4.toString()));
            }
            if (cell5 != null){
                excel.setUnitCode(cell5.toString());
            }
            if (cell6 != null){
                excel.setFloor(Integer.valueOf(cell6.toString()));
            }
            if (cell7 != null){
                excel.setDoorplateCode(cell7.toString());
            }
            if (cell8 != null){
                excel.setApartmentNumber(cell8.toString());
            }
            if (cell9 != null){
                excel.setArchitectureArea(cell9.toString());
            }
            if (cell10 != null){
                excel.setIndoorArea(cell10.toString());
            }
            if (cell11 != null){
                excel.setPublicArea(cell11.toString());
            }
            if (cell12 != null){
                excel.setRoomRate(cell12.toString());
            }
            if (cell13 != null){
                excel.setApartmentStructure(cell13.toString());
            }
            if (cell14 != null){
                excel.setActualUseArea(cell14.toString());
            }
            if (cell15 != null){
                excel.setPropertyTye(cell15.toString());
            }
            if (cell16 != null){
                excel.setApartmentStructure(cell16.toString());
            }
            if (cell17 != null){
                excel.setFace(cell17.toString());
            }
            if (cell18 != null){
                excel.setCourtyardArea(cell18.toString());
            }
            if (cell19 != null){
                excel.setRecordPrice(cell19.toString());
            }
            if (cell20 != null){
                excel.setRecordTotalPrice(cell20.toString());
            }
            if (cell21 != null){
                excel.setOldPrice(cell21.toString());
            }
            if (cell22 != null){
                excel.setOldTotal(cell22.toString());
            }
            if (!cell23.equals("")){
                excel.setDiscountPrice(Integer.valueOf(cell23.toString()));
            }
            if (!cell24.equals("")){
                excel.setDiscountTotal(Integer.valueOf(cell24.toString()));
            }
            if (cell25.toString().length() > 0){
                excel.setNum(Integer.valueOf(cell25.toString()));
            }
            if (!cell26.equals("")){
                excel.setType(cell26.toString());
            }
            if (!cell27.equals("")){
                String uid = userRepository.findId(cell27.toString());
                if (uid != null){
                    excel.setRelatedAdviser(Integer.valueOf(uid));
                }
            }
            if (cell28.toString().length() > 0){
                String gid = customerRegistrationRepository.getGName(cell28.toString());
                if (gid != null){
                    excel.setRelatedParty(Integer.valueOf(gid));
                }
            }
            if (!cell29.equals("")){
                excel.setRemarks(cell29.toString());
            }
            if (!cell30.equals("")){
                excel.setProposal(cell30.toString());
            }
            *//*if (cell3 != null){
                excel.setStage(cell3.toString());
            }*//*
            //houseDetailRepository.save(excel);
            excel.setAddTime(new Date());
            list.add(excel);
        }
        houseDetailRepository.saveAll(list);//批量插入数据
        return 0;
    }*/
}
